import time

import numpy
import pandas
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.path import Path

from find_series_in_another_series import m_to_m

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.rcParams['toolbar'] = 'toolmanager'
# 有中文出现的情况，需要u'内容'


data = pd.read_csv('data/波峰数据testSuper.csv', sep=',')


lines = [
    [(80, 280), (80, 280), (80, 280), (80, 280), (80, 280), (80, 280)],
    [(754, 954), (1034, 1234), (1034, 1234), (1034, 1234), (1034, 1234), (1034, 1234)],
    [(1348, 1548), (1468, 1668), (1468, 1668), (1628, 1828), (1228, 1428), (1388, 1588)],
    [(1622, 1822), (1502, 1702), (1502, 1702), (1502, 1702), (1502, 1702), (1502, 1702)],
    [(2296, 2496), (2336, 2536), (2336, 2536), (2336, 2536), (2336, 2536), (2336, 2536)],
    [(2650, 2950), (2610, 2810), (2850, 3050), (3010, 3210), (3010, 3210), (3010, 3210)],
    [(3164, 3364), (3044, 3244), (3124, 3324), (3124, 3324), (3124, 3324), (3044, 3244)],
    [(3124, 3324), (3004, 3204), (3164, 3364), (3404, 3604), (3004, 3204), (3004, 3204)]
]

# column = data.columns.drop("Depth")
# #  画出匹配到的对应的几组曲线
# f, ax = plt.subplots(nrows=1, ncols=6, figsize=(12, 15))
# for i, colu in enumerate(column):
#     ax[i].plot(data[colu], data['Depth'])
#     # ax[i].set_ylim(data['Depth'].min(), data['Depth'].max())
#     ax[i].invert_yaxis()  # 反转Y轴
#     ax[i].grid()
#
# plt.show()
#
# for line in lines:
#     f, ax = plt.subplots(nrows=1, ncols=6, figsize=(12, 15))
#     for i, sta_end in enumerate(line):
#         ax[i].plot(data[column[i]][sta_end[0]:sta_end[1]], data['Depth'][sta_end[0]:sta_end[1]])
#         ax[i].set_ylim(data['Depth'][sta_end[0]:sta_end[1]].min(), data['Depth'][sta_end[0]:sta_end[1]].max())
#         ax[i].invert_yaxis()  # 反转Y轴
#         ax[i].grid()
#
#     plt.show()


def curve_to_broken_line(curve, number_of_interpolations=50) -> pandas.Series:
    """
    ! 此方法没有返回原来序列对应的索引 也只有极值点
    将一个曲线模糊化，变成有极值点的曲线。返回panda.Series序列
    :param curve:给的曲线
    :param number_of_interpolations: 插值点的个数，也可以根据曲线长度取个数
    :return: 折线序列，从曲线中取那几个点
    """
    to_line = [curve[0]]

    for i, data in enumerate(curve[1:-1], 1):
        # 判断是否是随机取得点的位置
        # 判断两个线段的导数符号是否一致,异或运算
        if (curve[i+1]-curve[i] > 0) ^ (curve[i]-curve[i-1] > 0):
            to_line.append(data)
    # 取最后一个索引
    to_line.append(curve[curve.index[-1]])

    return pandas.Series(to_line)


if __name__ == '__main__':

    # plt.plot(curve_to_broken_line(data.S2))
    # plt.show()
    # plt.plot(curve_to_broken_line(data.S1[2300:2500].reset_index(drop=True)))
    # plt.show()
    # plt.plot(data.S1[2300:2500])
    # plt.show()
    #
    # plt.plot(data.S1)
    # plt.show()

    # plt.plot(curve_to_broken_line(data.S4)[1759:1764])
    # plt.show()
    # plt.plot(data.S2)
    # plt.show()

    plt.plot(curve_to_broken_line(data.S1))
    plt.title("提取极值点的图像")
    plt.show()
    plt.plot(data.S1)
    plt.title("原图像图像")
    plt.show()

    plt.plot(curve_to_broken_line(data.S1)[285:308])
    plt.title("经过提取极值点匹配到的范围")
    plt.show()
    plt.plot(data.S1[2300:2500])
    plt.title("匹配模板")
    plt.show()
    plt.plot(curve_to_broken_line(data.S1[2300:2500].reset_index(drop=True)))
    plt.title("匹配模板提取极值点之后的形状")
    # pd.read_csv( "波峰数据testSuper.csv",sep="," )

    # from testPlot import multiprocess_dtw
    # start = time.clock()
    # # 285:308
    # print(multiprocess_dtw(curve_to_broken_line(data.S1[2300:2500].reset_index(drop=True)), curve_to_broken_line(data.S1),
    #                        zoom_limits=50, min_scope=5, step=1, zoom_step=1))
    # end = time.clock()
    # print("%.03f seconds" % (end - start))

    # 结果
    # [(65, 77), (432, 452), (432, 442), (432, 452), (430, 444), (430, 444), (432, 446), (430, 446), (430, 448),
    #  (432, 452), (432, 450), (436, 452), (434, 450), (434, 452), (438, 452), (432, 450), (436, 450), (412, 430),
    #  (452, 462), (434, 450)]
    data2 = pd.read_csv('data/data.csv', sep=',')
    print(m_to_m(data2[420:440][data2.columns[30:50]], data2[350:][data2.columns[50:70]], deviation=100, zoom_limits=40, min_scope=10, zoom_step=2, step=2))
    plt.show()
